/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.sdw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.dsw2.biblioteca8a.bl.AutorBlLocal;
import mx.itschapala.dsw2.biblioteca8a.modelo.Autor;

/**
 *
 * @author Maricela
 */
@ManagedBean
@SessionScoped
public class AuthorBean {
    ///------ EJB-------------------------
    @EJB
    private AutorBlLocal autorBl;
    
    ///------variables o propiedades-------
    private List<Autor> lista;
    private Autor author;
    private String accion;
    ///------constructor-----------
    public AuthorBean() {
        author=new Autor();
    }
    ///------metodos GET y SET---------
    public List<Autor> getLista() {
        lista=autorBl.getAll();
        return lista;
    }
    public Autor getAuthor() {
        return author;
    }

    public void setAuthor(Autor author) {
        this.author = author;
    }
    //----- Acciones-------------------
    public String procesarAccion (){
    if (accion.equalsIgnoreCase("Nuevo")){
     autorBl.register(author);
    }else {
        if (accion.equalsIgnoreCase("Editar")){
    autorBl.modify(author);
    }else if (accion.equalsIgnoreCase("Eliminar")){
    autorBl.delete(author);
    }
    }
    accion ="";
    return "Autor";
    }
    
    //--------Listeners---------------
    public void prepararNuevo (ActionEvent evt){
    author= new Autor ();
    accion="Nuevo";
    }
    public void prepararEditar (ActionEvent evt){
//        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
//        System.out.println(index);
//        author=lista.get(index);
        accion = "Editar";
    }
    
    public void prepararEliminar (ActionEvent evt){
//        int index=Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
//        System.out.println(index);
//        author=lista.get(index);
        accion = "Eliminar";
    }

    

    
}
